package com.mediatek.factorymode.touchscreen1;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.mediatek.factorymode.R;
import android.view.Window;
import android.view.WindowManager;

public class DrawLineView
        extends Activity
{
    MyView v = null;

    public void onCreate(Bundle paramBundle)
    {
        super.onCreate(paramBundle);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

        setContentView(new MyView(this));
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }

    protected void onDestroy()
    {
        super.onDestroy();
    }

    protected void onPause()
    {
        super.onPause();
    }

    protected void onStop()
    {
        super.onStop();
    }

    private class MyView
            extends View {
        private List<Point> allPoints = new ArrayList();
        private int index = 0;
        private ArrayList<DrawLine> lineList = new ArrayList();
        private Paint paint = null;
        private float screenHeight;
        private double screenHypotenuse;
        private float screenWidth;
        private int state = 1;
        private float pOffset = 40.0F;

        public MyView(Context paramContext) {
            super(paramContext);
            super.setOnTouchListener(new OnTouchListenerImp());
            WindowManager wm = (WindowManager) paramContext.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            DisplayMetrics dm = new DisplayMetrics();
            display.getRealMetrics(dm);
            screenWidth = dm.widthPixels;
            screenHeight = dm.heightPixels;
            float scale = 1.0F;
            if (screenWidth < 481) {
                scale = 1.0F;
            } else if (screenWidth < 801) {
                scale = 1.5F;
            } else if (screenWidth < 1081) {
                scale = 2.0F;
            } else if (screenWidth < 1681) {
                scale = 3.0F;
            }
            pOffset = pOffset * scale;

            this.screenHypotenuse = Math.sqrt(this.screenWidth * this.screenWidth + this.screenHeight * this.screenHeight);
        }

        public boolean checkOutScope(float fx, float fy) {
            float f1 = fx * this.screenHeight + fy * this.screenWidth - this.screenHeight * this.screenWidth;
            f1 = f1 * f1;

            float f2 = fx * this.screenHeight - fy * this.screenWidth;
            f2 = f2 * f2;

            float f3 = pOffset * this.screenHeight;
            f3 = f3 * f3;

            if ((f1 >= f3 ) &&(f2 >= f3)) {
                return true;
            }
            return false;
        }

        public void draw(Canvas paramCanvas) {
            paramCanvas.drawColor(Color.BLACK);
            this.paint = new Paint();
            this.paint.setStyle(Paint.Style.FILL);
            this.paint.setStrokeWidth(2.0F);
            DrawLine drawLine = null;
            if (this.state ==1) {
                this.paint.setColor(Color.GREEN);
            } else if (this.state == 2) {
                this.paint.setColor(Color.BLUE);
            } else if(this.state == 3) {
                this.paint.setColor(Color.RED);
            }

            paramCanvas.drawLine(pOffset, 0.0F, this.screenWidth, (this.screenWidth - pOffset) * this.screenHeight / this.screenWidth, this.paint);
            paramCanvas.drawLine(0.0F, this.screenHeight * pOffset / this.screenWidth, this.screenWidth - pOffset, this.screenHeight, this.paint);
            paramCanvas.drawLine(this.screenWidth - pOffset, 0.0F, 0.0F, (this.screenWidth - pOffset) * this.screenHeight / this.screenWidth, this.paint);
            paramCanvas.drawLine(this.screenWidth, this.screenHeight * pOffset / this.screenWidth, pOffset, this.screenHeight, this.paint);
            Iterator localIterator = this.lineList.iterator();
            while ( localIterator.hasNext()) {
                drawLine = (DrawLine) localIterator.next();
                paramCanvas.drawLine(drawLine.first_x, drawLine.first_y, drawLine.last_x, drawLine.last_y, this.paint);
            }

            Point point1 = null;
            Point point2 = null;
            if (MyView.this.allPoints.size() > 1) {
                localIterator = MyView.this.allPoints.iterator();
                while ( localIterator.hasNext()) {
                    if (point2 != null) {
                        point1 = point2;
                    }
                    point2  = (Point) localIterator.next();
                    if (point1 != null) {
                        paramCanvas.drawLine(point1.x, point1.y, point2.x, point2.y, this.paint);
                    }
                }
            }
        }

        private class DrawLine {
            private float first_x;
            private float first_y;
            private float last_x;
            private float last_y;

            public DrawLine(float paramFloat1, float paramFloat2, float paramFloat3, float paramFloat4) {
                this.first_x = paramFloat1;
                this.first_y = paramFloat2;
                this.last_x = paramFloat3;
                this.last_y = paramFloat4;
            }
        }

        private class OnTouchListenerImp
                implements View.OnTouchListener {
            private float ex = 0.0F;
            private float ey = 0.0F;
            private double slope = 0.0D;
            private float sx = 0.0F;
            private float sy = 0.0F;

            private OnTouchListenerImp() {
            }

            public boolean onTouch(View v, MotionEvent event) {
                Point point = new Point((int) event.getX(), (int) event.getY());
                if (event.getPointerCount() > 1) {
                    return true;
                }
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    DrawLineView.MyView.this.state = 2;
                    this.sx = event.getX();
                    this.sy = event.getY();
                    DrawLineView.MyView.this.allPoints.add(point);
                } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    DrawLineView.MyView.this.allPoints.add(point);
                    DrawLineView.MyView.this.postInvalidate();
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    DrawLineView.MyView.this.allPoints.add(point);
                    DrawLineView.MyView.this.postInvalidate();
                    this.ex = event.getX();
                    this.ey = event.getY();
                    Iterator iterator = null;
                    boolean bCheckFailed = false;

                    if (DrawLineView.MyView.this.allPoints.size() > 10) {
                        iterator = MyView.this.allPoints.iterator();
                        while (iterator.hasNext()) {
                            Point localPoint = (Point) iterator.next();
                            if (DrawLineView.MyView.this.checkOutScope(localPoint.x, localPoint.y)) {
                                bCheckFailed = true;
                                break;
                            }
                        }

                        if (DrawLineView.MyView.this.index == 0) {
                            DrawLineView.MyView.this.index = 1;
                        } else if (DrawLineView.MyView.this.index == 1) {
                            DrawLineView.MyView.this.index = 2;
                        } else {
                            DrawLineView.MyView.this.index = 0;
                        }
                        if (!bCheckFailed) {
                            bCheckFailed = DrawLineView.MyView.this.checkOutScope(event.getX(), event.getY());
                        }

                        if (!bCheckFailed) {
                            if (Math.sqrt((this.ex - this.sx) * (this.ex - this.sx) + (this.ey - this.sy) * (this.ey - this.sy)) > DrawLineView.MyView.this.screenHypotenuse * 0.7D) {
                                if (DrawLineView.MyView.this.index == 1) {
                                    this.slope = ((this.ey - this.sy) / (this.ex - this.sx));
                                } else if (DrawLineView.MyView.this.index == 2) {
                                    double d = (this.ey - this.sy) / (this.ex - this.sx);
                                    if (this.slope * d < 0.0D) {
                                        DrawLineView.MyView.this.getContext().sendBroadcast(new Intent("com.mediatek.factorymode.touchscreen.test.action.finish").putExtra("TestPass", 3));
                                        DrawLineView.this.finish();
                                    } else {
                                        bCheckFailed = true;
                                    }
                                }
                                DrawLineView.MyView.this.state = 3;
                            } else {
                                bCheckFailed = true;
                            }
                        }
                    } else { // less than 10 points
                        bCheckFailed = true;
                    }
                    Iterator localIterator;
                    Point point1 = null;
                    Point point2 = null;

                    if (MyView.this.allPoints.size() > 1) {
                        localIterator = MyView.this.allPoints.iterator();
                        while ( localIterator.hasNext()) {
                            if (point2 != null) {
                                point1 = point2;
                            }
                            point2  = (Point) localIterator.next();
                            if (point1 != null) {
                                MyView.this.lineList.add(new DrawLine(point1.x, point1.y, point2.x, point2.y));
                            }
                        }
                    }
                    DrawLineView.MyView.this.allPoints.clear();

                    if (bCheckFailed) {
                        DrawLineView.MyView.this.state = 3;
                        DrawLineView.MyView.this.index = 0;
                        Toast.makeText(DrawLineView.this, R.string.drawLine_false, Toast.LENGTH_SHORT).show();
                        DrawLineView.MyView.this.lineList.clear();
                        DrawLineView.MyView.this.postInvalidate();
                    }
                }
                return true;
            }
        }
    }
}
